package com.study.mall.function.yinyong;

import com.study.mall.function.pojo.Banji;
import com.study.mall.function.pojo.Student;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;

public class Demo {
    @Test
    public void test12() {
        List<Banji> banjis = getBanjis();
        //类::静态方法
        //对象::实例方法
        //实现接口的抽象方法的参数列表和返回值类型，必须与方法引用的方法的参数列表和返回值类型保持一致！
        //Consumer  void accept(T t)
        //PrintStream中的void println(T t)

        banjis.stream()
                .flatMap(banji -> banji.getStudents().stream())
                .map(student -> student.getAge())
                .map(new Function<Integer, String>() {
                    @Override
                    public String apply(Integer age) {
                        return String.valueOf(age);
                    }
                })
                .forEach(new Consumer<String>() {
                    @Override
                    public void accept(String age) {
                        System.out.println(age);
                    }
                });

        banjis.stream()
                .flatMap(banji -> banji.getStudents().stream())
                .map(student -> student.getAge())
                .map(age -> String.valueOf(age))
                .forEach(age -> System.out.println(age));

        banjis.stream()
                .flatMap(banji -> banji.getStudents().stream())
                .map(student -> student.getAge())
                .map(String::valueOf)
                .forEach(System.out::println);
    }

    @Test
    public void test13() {
        StringBuilder stringBuilder = new StringBuilder();

        //类::实例方法
        //当函数式接口方法的第一个参数是需要引用方法的调用者，并且第二个参数是需要引用方法的参数(或无参数)时
        List<Banji> banjis = getBanjis();
        banjis.stream()
                .flatMap(banji -> banji.getStudents().stream())
                .map(new Function<Student, String>() {
                    @Override
                    public String apply(Student student) {
                        return student.getName();
                    }
                })
                .forEach(new Consumer<String>() {
                    @Override
                    public void accept(String name) {
                        stringBuilder.append(name);
                    }
                });


        banjis.stream()
                .flatMap(banji -> banji.getStudents().stream())
                .map(Student::getName)
                .forEach(stringBuilder::append);

    }

    @Test
    public void test555() {
        String[] strings = {"apple", "banana", "cherry"};
        // 使用 Lambda 表达式
        Arrays.sort(strings, new Comparator<String>() {
            @Override
            public int compare(String s1, String s2) {
                return s1.compareTo(s2);
            }
        });
        Arrays.sort(strings, (s1, s2) -> s1.compareToIgnoreCase(s2));
        Arrays.sort(strings, String::compareTo);
    }


    @Test
    public void test54() {
        List<Banji> banjis = getBanjis();
        banjis.stream()
                .flatMap(banji -> banji.getStudents().stream())
                .map(student -> student.getName())
                .map(new Function<String, StringBuilder>() {
                    @Override
                    public StringBuilder apply(String name) {
                        return new StringBuilder(name);
                    }
                })
                .map(sb -> sb.append("-java"))
                .forEach(str -> System.out.println(str));

        banjis.stream()
                .flatMap(banji -> banji.getStudents().stream())
                .map(Student::getName)
                .map(StringBuilder::new)
                .map(sb -> sb.append("-java"))
                .forEach(System.out::println);
    }

    public List<Banji> getBanjis() {
        Banji banji1 = new Banji(1, "Java2307", null);
        Banji banji2 = new Banji(2, "UI2307", null);
        Banji banji3 = new Banji(3, "H52307", null);

        //学生列表
        List<Student> students1 = new ArrayList<>();
        List<Student> students2 = new ArrayList<>();
        List<Student> students3 = new ArrayList<>();

        students1.add(new Student(1, "张三1", 23));
        students1.add(new Student(2, "张三2", 33));
        students1.add(new Student(3, "张三3", 16));
        students1.add(new Student(3, "张三3", 16));

        students2.add(new Student(4, "李四1", 123));
        students2.add(new Student(5, "李四2", 33));
        students2.add(new Student(6, "李四3", 16));

        students3.add(new Student(7, "王五1", 23));
        students3.add(new Student(8, "王五2", 33));
        students3.add(new Student(9, "王五3", 16));

        banji1.setStudents(students1);
        banji2.setStudents(students2);
        banji3.setStudents(students3);

        List<Banji> banjis = Arrays.asList(banji1, banji2, banji3);
        return banjis;
    }
}
